package com.activity.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.activity.bean.ClassSpan;
import com.activity.bean.TimeBean;
import com.activity.fragment.NewClassActivity;
import com.activity.shop.ClassActivity;
import com.donkingliang.groupedadapter.holder.BaseViewHolder;
import com.example.apple.ddhr_new.R;
import com.example.apple.ddhr_new.adapter.BaseAdapter;
import com.example.apple.ddhr_new.utils.ActivityUtil;
import com.example.apple.ddhr_new.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import butterknife.BindView;

public class TimeChooseAdapter extends BaseAdapter<TimeBean> {


    public TimeChooseAdapter(Context context) {
        super(context);
    }
    public TimeListener  timeListener ;

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return true;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.header_time_choose;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return R.layout.footer_time_shoose;
    }

    @Override
    public BaseViewHolder<TimeBean> getViewHolder(int viewType, View view) {
        if(viewType==TYPE_CHILD)
        return new TimeChooseViewHolder(view);
        return null;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_time_choose_cell;
    }

    @Override
    public void onBindChildViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition, int childPosition) {
        super.onBindChildViewHolder(holder,groupPosition,childPosition);
        TimeChooseViewHolder  classHolder = (TimeChooseViewHolder) holder;
        classHolder.ivCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(getData().get(childPosition).getClassSpan()==null){

                    ToastUtil.toast(mContext,"请编辑班次");
                }else {
                    v.setSelected(!v.isSelected());
                    getData().get(childPosition).selected = !getData().get(childPosition).selected;
                }


            }
        });
        classHolder.ivEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(timeListener==null){

                }else {
                    timeListener.editClick(childPosition);
                }

            }
        });

    }

    @Override
    public void onBindHeaderViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {

    }

    @Override
    public void onBindFooterViewHolder(com.donkingliang.groupedadapter.holder.BaseViewHolder holder, int groupPosition) {
        super.onBindFooterViewHolder(holder, groupPosition);
        holder.itemView.findViewById(R.id.btn_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if(timeListener==null){

                }else {
                    timeListener.doneClick();
                }
            }
        });
    }

    public class TimeChooseViewHolder extends BaseViewHolder<TimeBean> {

        @BindView(R.id.iv_check)
        ImageView ivCheck;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_placeHolder)
        TextView tvPlaceHolder;
        @BindView(R.id.iv_edit)
        ImageView ivEdit;

        public TimeChooseViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        @Override
        public void setData(TimeBean data) {

            tvName.setText(data.getName());
            ivCheck.setSelected(data.selected);
            if(data.getClassSpan()!=null){
                tvPlaceHolder.setGravity(Gravity.LEFT);
                tvPlaceHolder.setText(data.getClassSpan().getAttendacetimespanname());
            }else {
                if(data.getPlaceHolder().equals("休息")){
                    tvPlaceHolder.setGravity(Gravity.RIGHT);
                }else {
                    tvPlaceHolder.setGravity(Gravity.LEFT);
                }
                tvPlaceHolder.setText(data.getPlaceHolder());

            }

        }
    }

    public  interface  TimeListener{
        public  void  editClick(int ChildPosition);
        public  void  doneClick();
    }

}
